package com.hxkcloud.nurse.activitys;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hxkcloud.nurse.R;
import com.hxkcloud.nurse.adapter.DoctorEvalutionAdapter;
import com.hxkcloud.nurse.beans.DoctorAssessBean;
import com.hxkcloud.nurse.beans.DoctorAssessWholeBean;
import com.hxkcloud.nurse.config.UrlConfig;
import com.hxkcloud.nurse.recorder.MediaManger;
import com.hxkcloud.nurse.request.MessageCode;
import com.hxkcloud.nurse.request.YiJiaHuNetWorkUtils;
import com.hxkcloud.nurse.utils.ImageVideoUtils;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullToRefreshLayout;
import com.hxkcloud.nurse.views.pulltorefreshlayout.PullableRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by william_sun on 2016-10-09.
 * Description:患者端医生评价列表
 */
public class DoctorEvalutionActivity extends BaseActivity implements View.OnClickListener, PullToRefreshLayout.OnRefreshListener {

    //title
    private LinearLayout ll_title;
    private ImageView iv_back;
    private TextView tv_title;

    private PullToRefreshLayout refreshLayout;
    private PullableRecyclerView recyclerView;

    private List<DoctorAssessBean> datas;
    private DoctorEvalutionAdapter adapter;
    private int totalCount = 0;

    private int voicePosition;
    private int pageIndex = 1;
    private int pageSize = 15;

    private LinearLayout nodataLayout;
    private LinearLayout infoLayout;


    @Override
    protected void updateByHandler(Message msg) {
        switch (msg.what) {
            case MessageCode.NETWORKCODE_DOWNLOADVOICE_SUCCESS:
//                File file = (File) msg.obj;
                if (!MediaManger.isPlaying()) {
                    datas.get(voicePosition).setIsVoicePlaying(true);
                    adapter.notifyItemChanged(voicePosition);
//                    adapter.setData(datas);
                    MediaManger.playSound(ImageVideoUtils.getDataCacheDir(mContext) + "/voice.amr", new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mp) {
                            datas.get(voicePosition).setIsVoicePlaying(false);
//                            adapter.setData(datas);
                            adapter.notifyItemChanged(voicePosition);
                        }
                    });
                }
                break;
            case MessageCode.NETWORKCODE_DOWNLOADVOICE_FAILURE:
                toastMsg(getResources().getString(R.string.voicedownloadfail));
                break;
            case MessageCode.NETWORKCODE_DOCTORASSESS_SUCCESS:
                if (pageIndex == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                    datas.clear();
                } else {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                DoctorAssessWholeBean wholeBean = (DoctorAssessWholeBean) msg.obj;
                datas.addAll(wholeBean.getList());
                if (!TextUtils.isEmpty(wholeBean.getTotalCount())) {
                    totalCount = Integer.valueOf(wholeBean.getTotalCount());
                }
                adapter.setData(datas);
                break;
            case MessageCode.NETWORKCODE_DOCTORASSESS_FAILURE:
                if (pageIndex == 1) {
                    refreshLayout.refreshFinish(PullToRefreshLayout.SUCCEED);
                } else {
                    refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
                }
                if ((msg.obj != null) && (!TextUtils.isEmpty((String) msg.obj))) {
                    toastMsg((String) msg.obj);
                } else {
                    toastMsg(R.string.datagetfail);
                }
                break;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctorevalution);

        initView();
    }

    @Override
    public void onResume() {
        super.onResume();
        initData();
    }

    @Override
    protected void initView() {
        ll_title = (LinearLayout) findViewById(R.id.ll_title);
        iv_back = (ImageView) findViewById(R.id.iv_title_back);
        tv_title = (TextView) findViewById(R.id.tv_title_title);

        setTitlePadding(ll_title);
        iv_back.setVisibility(View.VISIBLE);
        tv_title.setText(getResources().getText(R.string.doctorevalution));
        iv_back.setOnClickListener(this);

        refreshLayout = (PullToRefreshLayout) findViewById(R.id.refreshlayout_evalution);
        recyclerView = (PullableRecyclerView) findViewById(R.id.recyclerview_evalution);

        nodataLayout = (LinearLayout) findViewById(R.id.ll_department_empty);
        infoLayout = (LinearLayout) findViewById(R.id.ll_evalution_info);

        LinearLayoutManager mLayoutManager = new LinearLayoutManager(mContext);
        mLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLayoutManager);
        refreshLayout.setOnRefreshListener(this);

        datas = new ArrayList<>();
        adapter = new DoctorEvalutionAdapter(mContext, datas, new DoctorEvalutionAdapter.onEvalutionVoiceListener() {
            @Override
            public void onVoice(int position) {
                if (!MediaManger.isPlaying()) {
                    String url = UrlConfig.image_host + datas.get(position).getVoiceUrl();
                    YiJiaHuNetWorkUtils.downloadFile(url, mContext, handler);
                    voicePosition = position;
                }
            }
        });
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected void initData() {
        loading();
        pageIndex = 1;
        YiJiaHuNetWorkUtils.getDoctorAssessList(pageIndex, pageSize, handler);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_title_back:
                finish();
                break;
        }
    }

    @Override
    public void onRefresh(PullToRefreshLayout pullToRefreshLayout) {
        initData();
    }

    @Override
    public void onLoadMore(PullToRefreshLayout pullToRefreshLayout) {
        if (datas.size() >= totalCount) {
            refreshLayout.loadmoreFinish(PullToRefreshLayout.SUCCEED);
            toastMsg(R.string.nomore);
        } else {
            loading();
            pageIndex++;
            YiJiaHuNetWorkUtils.getDoctorAssessList(pageIndex, pageSize, handler);
        }
    }
}
